<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/7
 * Time: 18:07
 */
class SchoolBookDinnderMsgtpl extends BaseMsgtpl
{

    /**
     * 发送消息给用户
     * @param $orderInfo
     * @param $accountId
     * @param int $type 1.发送下单消息,2发送领餐消息
     * @return bool
     */
    public function sendMessage($orderInfo, $accountId, $type = 1)
    {
        //查询订单信息
        $orderGoodsInfo = D('Schoolirs/SchoolOderGoods')->getList(array('order_id' => $orderInfo['order_id']), array('goods_name', 'goods_num'));

        $condition = array(
            'user_id' => $orderInfo['user_id'],
        );

        //查询微信用户信息
        $wechatUser = D('Wechat/WechatUser')->getInfo($condition, 'openid,subscribe');

        $url = C('site_url') . "/Schoolirs/Order/orderDetail/res_id/" . $orderInfo['res_id'] . "/order_id/" . $orderInfo['order_id'];

        //
        $keyword3 = '';
        if ($orderGoodsInfo) {
            foreach ($orderGoodsInfo as $key => $v) {
                $keyword3 = $keyword3 .$v['goods_num'].'份'.$v['goods_name']. ' , ';
            }
        }

        $time = date('H:i',strtotime($orderInfo['take_time']));
        if($type==1){//下单
            $remark = '  您的预订用餐时间为'.$time.'，请及时领取!';
        }else{//领餐
            $remark = '  欢迎下次光临!';
        }

        //查询餐厅消息设置
        $messageInfo = D('Schoolirs/RefectoryWindowMessage')->getInfo(array('window_id' => $orderInfo['window_id'], 'message_type' => $type));

        $arrayMessage =  array(
          '您已预订成功!',
          '您已预订成功！',
          '您已领取成功！'
        );

        if ($wechatUser['subscribe']) {
            if ($accountId == 1) {
                $this->wxTplSign = 'BypiRvSt1PW5vTRbvukx0kYtPrh7_naMEFwqhhmgU4w';
            } elseif ($accountId == 2) { //滨江后勤
                $this->wxTplSign = '4nPGAerxGLLq_5jq1Q5ugiihOsS8iRTMcQbwTJdd3dw';
            } elseif ($accountId == 5) {  //农林
                $this->wxTplSign = 'En2hbZ6GDZsh3DR_DaQWlFsr98HUDU9kLBkolfcVN-A';
            } elseif ($accountId == 6) {  //暨阳学院
                $this->wxTplSign = 'sU-dMfDOcDkx2Or7L45JhDPG-5fbZ4LjtBjyvoodbOs';
            } elseif ($accountId == 25) { // 订餐
                $this->wxTplSign = 'ahcFELk4XN7HvaT1GqEqsuwhhEgwFAPtjl5UNxy7Tg8';
            } elseif ($accountId == 28) { //云感
                $this->wxTplSign = 'keHCeWQVgErMeb69TuxDEmt7ErroQnxwLPXVEb-YwuQ';
            }elseif  ($accountId==38) { // 中医药大学
                $this->wxTplSign = 'vUoBb0lvMHCMQn-YKAMwkbhOadAJNAHRR7rQDX18Eko';
            }

            if ($accountId = 28) {//云感
                $info = array(
                    'first' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : $arrayMessage[$type]),
                    'keyword1' => array("value" => '校园订餐'),
                    'keyword2' => array("value" => date("Y-m-d H:i:s")),
                    'remark' => array("value" => $keyword3.$remark),
                );
            } else {
                $info = array(
                    'first' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : $arrayMessage[$type]),
                    'keyword1' => array("value" => '校园订餐'),
                    'keyword2' => array("value" => date("Y-m-d H:i:s")),
                    'keyword3' => array("value" => $keyword3),
                    'remark' => array("value" => $remark),
                );
            }

            $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $this->wxTplSign, $info, $url);
        }
        return true;
    }

    /**
     * 发送消息给餐厅管理员
     * @param string $orderInfo 订单信息
     * @param string $accountId 账号id
     */
    public function sendMessageToManage($orderInfo, $accountId)
    {

        try {
            $managerUsers = D('Schoolirs/SchoolRefectoryAccount')->getList(array('window_id' => $orderInfo['window_id'], 'oprate_right' => array('in', array(0, 2)), 'cut_off' => 0), array('user_id'));
            if ($managerUsers) {

                $url = C('site_url') . "/Schoolirs/Manage/sendList/res_id/" . $orderInfo['res_id'] . '/refectory_id/' . $orderInfo['refectory_id'] . '/window_id/' . $orderInfo['window_id'];

                //查询订单信息
                $orderGoodsInfo = D('Schoolirs/SchoolOderGoods')->getList(array('order_id' => $orderInfo['order_id']), array('goods_name', 'goods_num'));

                $keyword3 = '';
                if ($orderGoodsInfo) {
                    foreach ($orderGoodsInfo as $key => $v) {
                        $keyword3 = $keyword3 . $v['goods_num'] . '份' . $v['goods_name'] . ' , ';
                    }
                }
                $time = date('H:i', strtotime($orderInfo['take_time']));

                $remark = ' 预订用餐时间' . $time . '，请及时备餐!';


                //查询餐厅消息设置
                $messageInfo = D('Schoolirs/RefectoryWindowMessage')->getInfo(array('window_id' => $orderInfo['window_id'], 'message_type' => 3));
                wLog('messageInfo:'.var_export($messageInfo,true),'SchoolBookDinnerMsgtpl');

                if (!$messageInfo) {
                    return;
                }

                if ($accountId = 28) {//云感
                    $info = array(
                        'first' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : '您有一条新订单！'),
                        'keyword1' => array("value" => '校园订餐'),
                        'keyword2' => array("value" => date("Y-m-d H:i:s")),
                        'remark' => array("value" => $keyword3 . $remark),
                    );
                } else {
                    $info = array(
                        'first' => array("value" => $messageInfo['message_content'] ? $messageInfo['message_content'] : '您有一条新订单！'),
                        'keyword1' => array("value" => date("Y-m-d H:i:s")),
                        'keyword2' => array("value" => $keyword3),
                        'remark' => array("value" => $remark),
                    );
                }


                foreach ($managerUsers as $managerUser) {

                    $wechatUser = D('Wechat/WechatUser')->getInfo(array('user_id' => $managerUser['user_id'], 'wechat_id' => $this->wechatId), 'openid,subscribe');
                    wLog('wechatUser:'.var_export($wechatUser,true),'SchoolBookDinnerMsgtpl');

                    if (!$wechatUser) {
                        continue;
                    }

                    if ($wechatUser['subscribe']) {
                        if ($accountId == 2) { //滨江后勤
                            $this->wxTplSign = '4nPGAerxGLLq_5jq1Q5ugiihOsS8iRTMcQbwTJdd3dw';
                        } elseif ($accountId == 5) {  //农林
                            $this->wxTplSign = 'En2hbZ6GDZsh3DR_DaQWlFsr98HUDU9kLBkolfcVN-A';
                        } elseif ($accountId == 6) {  //暨阳学院
                            $this->wxTplSign = 'sU-dMfDOcDkx2Or7L45JhDPG-5fbZ4LjtBjyvoodbOs';
                        } elseif ($accountId == 25) { // 订餐
                            $this->wxTplSign = 'ahcFELk4XN7HvaT1GqEqsuwhhEgwFAPtjl5UNxy7Tg8';
                        } elseif ($accountId == 28) { // 云感
                            $this->wxTplSign = 'keHCeWQVgErMeb69TuxDEmt7ErroQnxwLPXVEb-YwuQ';
                        } elseif ($accountId == 38) { // 中医药大学
                            $this->wxTplSign = 'yL2U1uUe15h0N1PMZwg5MASSA_Hj9DgTad1e-v1DOzo';
                        }

                        $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $this->wxTplSign, $info, $url);
                    }

                }
            }
        } catch(ThinkException $e) {
            $errMsg = $e->getMessage();
            wLog('exception:'.$errMsg,'exception');
        }
    }
}